from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from .services import get_douban_limit_n,insert_type,get_douban_limit_n_by_score,get_douban_limit_n_by_title,get_douban_limit_n_by_rank,get_douban_limit_n_by_score_big,insert_movie,del_movie,updta_movies
import json
# Create your views here.

def get_douban_n(request):
    n = request.GET.get("n")
    res = get_douban_limit_n(n)
    print(res)
    print(n)
    return HttpResponse(res)
# Create your views here.



class Resp():
    def msg(self, code, msg, data=None):
        if data == None:
            resl = {'code': code, 'msg': msg}
        else:
            resl = {'code':code, 'msg':msg, 'data': data}
        return HttpResponse(json.dumps(resl, ensure_ascii=False), content_type='application/json; charset=utf-8')

    def success(self, msg='处理成功',data=None):
        return self.msg(code=0, msg=msg, data=data)

    def warn(self, msg='操作异常，请重试'):
        return self.msg(code=1, msg=msg)

    def error(self, msg='系统异常'):
        return self.msg(code=2, msg=msg)


def get_douban_n(request):
    n = request.GET.get("n")
    res = get_douban_limit_n(n)
    print(res)

    return Resp().success(msg='获取成功', data=res)

def get_douban_by_score(request):
    n = request.GET.get("n")
    res = get_douban_limit_n_by_score(n)
    print(res)

    return Resp().success(msg='获取成功', data=res)

def get_douban_by_score_big(request):
    n = request.GET.get("n")
    res = get_douban_limit_n_by_score_big(n)
    print(res)

    return Resp().success(msg='获取成功', data=res)

def get_douban_by_title(request):
    n = request.GET.get("n")
    res = get_douban_limit_n_by_title(n)
    print(res)

    return Resp().success(msg='获取成功', data=res)

def get_douban_by_rank(request):
    n = request.GET.get("n")
    res = get_douban_limit_n_by_rank(n)
    print(res)

    return Resp().success(msg='获取成功', data=res)





def add_type(request):
    name = request.POST.get('name')
    code = request.POST.get('code')
    insert_type(name,code)
    return Resp().success('插入成功')



def add_movie(request):
    # 检查请求方法是否为 POST 或 GET
    if request.method not in ['POST', 'GET']:
        return JsonResponse({'error': 'Only POST or GET method is allowed'}, status=405)

    # 从 POST 或 GET 请求中提取参数
    if request.method == 'POST':
        code = request.POST.get('code')
        movie_name = request.POST.get('movie_name')
        avg_audience_count = request.POST.get('avg_audience_count')
        release_time = request.POST.get('release_time')
        avg_boxoffice = request.POST.get('avg_boxoffice')
        boxoffice = request.POST.get('boxoffice')
        rank = request.POST.get('rank')
    elif request.method == 'GET':
        code = request.GET.get('code')
        movie_name = request.GET.get('movie_name')
        avg_audience_count = request.GET.get('avg_audience_count')
        release_time = request.GET.get('release_time')
        avg_boxoffice = request.GET.get('avg_boxoffice')
        boxoffice = request.GET.get('boxoffice')
        rank = request.GET.get('rank')

    insert_movie(code, movie_name, avg_audience_count, release_time, avg_boxoffice, boxoffice, rank)

    #

    if not all([code, movie_name, avg_audience_count, release_time, avg_boxoffice, boxoffice, rank]):
        return JsonResponse({'error': 'All fields are required'}, status=400)

    try:
        print(code, movie_name, avg_audience_count, release_time, avg_boxoffice, boxoffice, rank)
        # 调用插入数据库的函数
        insert_movie(code, movie_name, avg_audience_count, release_time, avg_boxoffice, boxoffice, rank)
        return JsonResponse({'message': '插入成功'}, status=200)
    except Exception as e:
        # 捕获并返回异常信息
        return JsonResponse({'error': str(e)}, status=500)


def del_movies(request):
    n = request.GET.get('code')
    print(n)
    print("TEST")
    del_movie(n)
    return Resp().success('删除成功')



def updata_movie(request):
    code = request.GET.get('code')
    name = request.GET.get('name')
    print("TEST")
    updta_movies(code,name)
    return Resp().success('更新成功')




